package com.ruoyi.project.asset.assets.controller;

import java.util.List;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.project.asset.assets.domain.Assets;
import com.ruoyi.project.asset.assets.service.IAssetsService;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.framework.web.page.TableDataInfo;

/**
 * 资产Controller
 * 
 * @author xianzw
 * @date 2020-08-24
 */
@Controller
@RequestMapping("/asset/assets")
public class AssetsController extends BaseController
{
    private String prefix = "asset/assets";

    @Autowired
    private IAssetsService assetsService;

    @RequiresPermissions("asset:assets:view")
    @GetMapping()
    public String assets()
    {
        return prefix + "/assets";
    }

    /**
     * 查询资产列表
     */
    @RequiresPermissions("asset:assets:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(Assets assets)
    {
        startPage();
        List<Assets> list = assetsService.selectAssetsList(assets);
        return getDataTable(list);
    }

    /**
     * 导出资产列表
     */
    @RequiresPermissions("asset:assets:export")
    @Log(title = "资产", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(Assets assets)
    {
        List<Assets> list = assetsService.selectAssetsList(assets);
        ExcelUtil<Assets> util = new ExcelUtil<Assets>(Assets.class);
        return util.exportExcel(list, "assets");
    }

    /**
     * 新增资产
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存资产
     */
    @RequiresPermissions("asset:assets:add")
    @Log(title = "资产", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(Assets assets)
    {
        return toAjax(assetsService.insertAssets(assets));
    }

    /**
     * 修改资产
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap)
    {
        Assets assets = assetsService.selectAssetsById(id);
        mmap.put("assets", assets);
        return prefix + "/edit";
    }

    /**
     * 修改保存资产
     */
    @RequiresPermissions("asset:assets:edit")
    @Log(title = "资产", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(Assets assets)
    {
        return toAjax(assetsService.updateAssets(assets));
    }

    /**
     * 删除资产
     */
    @RequiresPermissions("asset:assets:remove")
    @Log(title = "资产", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(assetsService.deleteAssetsByIds(ids));
    }
}
